<?php
/**
 * APC缓存
 *
 */
class Apc {
	private $_iTime;
	
	/**
	 * 构造函数
	 *
	 */
	public function __construct() {
		try {
			if (! extension_loaded ( 'apc' )) {
				throw new ExceptionExt ( 'APC模块没有安装' );
			}
			self::_setTime ();
		} catch ( ExceptionExt $e ) {
			$e->showError ();
		}
	}
	
	private function _setTime() {
		$this->_iTime = intval ( Base::getConf ( 'cache.time' ) );
	}
	
	/**
	 * 设置缓存
	 * $sKey缓存名
	 * $sValue缓存内容
	 * $sCategory缓存所属类型
	 *
	 * @param string $sKey
	 * @param string $sValue
	 * @param string $sCategory
	 */
	public function set($sKey, $sValue, $sCategory = null) {
		$sKey = $sCategory . '_' . $sKey;
		if ($sCategory !== null) {
			$iTime = Base::getConf ( $sCategory . '.cacheTime' );
			if ($iTime === null) {
				$iTime = $this->_iTime;
			}
		} else {
			$iTime = $this->_iTime;
		}
		
		if ($iTime == 0) {
			apc_store ( $sKey, serialize ( $sValue ) );
		} else {
			apc_store ( $sKey, serialize ( $sValue ), $iTime );
		}
	}
	
	/**
	 * 获取缓存
	 *
	 * @param string $sKey
	 * @param string $sCategory
	 */
	public function get($sKey, $sCategory = null) {
		$sKey = $sCategory . '_' . $sKey;
		$aData = apc_fetch ( $sKey );
		if ($aData === false) {
			return '__NOCACHE__';
		} else {
			return unserialize ( $aData );
		}
	}
}
?>